package com.caimiao.miao.page.welcome;

import android.Manifest;
import android.content.Context;

import com.caimiao.miao.bean.LocationInfo;
import com.caimiao.miao.http.rxjava.observer.CustomObserver;
import com.caimiao.miao.http.rxjava.transformer.ResponseTransformer;
import com.caimiao.miao.interfaces.BaseActivity;
import com.caimiao.miao.interfaces.BasePresenterImpl;
import com.caimiao.miao.interfaces.ICallback;
import com.caimiao.miao.http.rxjava.transformer.SchedulerTransformer;
import com.caimiao.miao.library.utils.tip.LogUtil;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class MainPresenter extends BasePresenterImpl<IMainContact.View, IMainContact.Model> implements IMainContact.Presenter {
    public MainPresenter(IMainContact.View mView) {
        super(mView);
    }

    @Override
    protected IMainContact.Model getModel() {
        return new MainModel();
    }

    @Override
    public void start(Context context) {
        RxPermissions rxPermissions = new RxPermissions((BaseActivity) context);
        Disposable disposable = rxPermissions.request(Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION)
                .compose(SchedulerTransformer.applyIoAndUi())
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        mModel.getLocation(context, new ICallback<LocationInfo>() {
                            @Override
                            public void success(LocationInfo data) {
                                onNext(context, data);
                            }

                            @Override
                            public void failed() {
                            }
                        });
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                });
        addDisposable(disposable);
    }

    private void onNext(Context context, LocationInfo locationInfo) {
        mModel.setDeviceInfo(context, locationInfo);
        mModel.postDevice(context)
                .compose(ResponseTransformer.handleResult2())
                .doOnNext(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mModel.backup(context);
                    }
                })
                .compose(SchedulerTransformer.applyIoAndUi())
                .subscribe(new CustomObserver<Object>(getCompositeDisposable()) {
                    @Override
                    public void onNext(Object o) {
                        LogUtil.e("TEST", "onNext: " + o);
                        if (mView != null) mView.skipActivity();
                    }
                });
    }
}
